<?php
class leonhere_hot_app_list extends WP_Widget {
	function __construct(){
		$widget_des = array('description' => '主题自带调用浏览最多的APP');
		parent::__construct('leonhere_hot_app_list',$name='主题 热门APP',$widget_des);
	}
	function form($instance){
		$instance = wp_parse_args((array)$instance,array('title'=>'热门APP','showPosts'=>'5'));
		$title = htmlspecialchars($instance['title']);		
		$showPosts = htmlspecialchars($instance['showPosts']);
		echo '<p style="text-align:left;"><label for="'.$this->get_field_name('title').'">显示标题:<input style="width:200px;" id="'.$this->get_field_id('title').'" name="'.$this->get_field_name('title').'" type="text" value="'.$title.'" /></label></p>';
		echo '<p style="text-align:left;"><label for="'.$this->get_field_name('showPosts').'">显示数量:<input style="width:200px;" id="'.$this->get_field_id('showPosts').'" name="'.$this->get_field_name('showPosts').'" type="text" value="'.$showPosts.'" /></label></p>';		
	}
	function update($new_instance,$old_instance){
		$instance = $old_instance;
		$instance['title'] = strip_tags(stripslashes($new_instance['title']));		
		$instance['showPosts'] = strip_tags(stripslashes($new_instance['showPosts']));		
		return $instance;
	}
	function the_hot_posts($args = ''){
		$default = array('meta_key' => 'views','orderby' => 'meta_value_num','order' => 'DESC','post_type' => 'post','showPosts'=>'5','post_status' => 'publish');
		$r = wp_parse_args($args,$default);
		extract($r);
		$args = array(
			'showposts'=>$showPosts,
			'orderby'=>'meta_value_num',
			'meta_key'=>'views',
			'ignore_sticky_posts'=>1,
			'post_type' => 'post',
			'post_status' => 'publish'
		);
		query_posts($args);		
		if(have_posts()){
			echo '<ul>';			
			while (have_posts()) {
				the_post();				
				echo '<li><figure class="icon"><a href="'.get_permalink().'" title="'.get_the_title().'" target="_blank"><img src="';
				get_template_part('functions/thumbnail');
				echo '" alt="'.get_the_title().'"/></a></figure><div class="info"><h4><a href="'.get_permalink().'" title="'.get_the_title().'" target="_blank">'.get_the_title().'</a></h4><p>'.wp_trim_words(get_the_content(), 24, "...").'</p></div></li>';
			}
			echo '</ul>';
		}
		wp_reset_query();
	}
	function widget($args, $instance){
		extract($args);
		$title = apply_filters('widget_title', empty($instance['title']) ? __('热门应用','leonhere') : $instance['title']);
		$title = $title;		
		$showPosts = empty($instance['showPosts']) ? 5 : $instance['showPosts'];		
		echo '<section class="hotapps">';
		if( $title ) echo $before_title . $title . $after_title;
		self::the_hot_posts("showPosts=$showPosts");
		echo '</section>';
	}
}
register_widget('leonhere_hot_app_list');
?>